
(function(w) {
	var i18n  = {};
	var store = new Store('pcc2-i18n-');
	var dlang = config.defaultLang;

	// var lang  = store.get('lang') || dlang;
	// 每次启动都是配置中的默认语言
	// var lang  = 'dlang';
	var lang  = 'en';

	i18n.getLang = function() {
		return lang;
	};
	i18n.setLang = function(l) {
		if(!l) return;
		lang = l;
		store.set('lang', l);
	};
	i18n.get = function(k, index) {
		index       = index || 0;
		var v       = LANG[k] ? (LANG[k][lang] || LANG[k]['en'] || k) : k;
		var array   = v.split('$$$$$');
		var isSplit = !!(array.length > 1);
		if(index >= array.length)
			index = 0;
		if(isSplit) {
			v = array[index] + ' <img class="'+ (index===0?'next':'prev') +'" src="./image/read-next.png" />';
		}

		var tpl = '<span class="i18n-word%s" data-langkey="%s" data-langindex="%d">%s</span>';
		return util.format(tpl, isSplit?' splited':'', k, isSplit?index:-1, v);
	};

	i18n._readMore = function($dom) {
		var index = $dom.data('langindex') - 0;
		var key   = $dom.data('langkey');
		if(index > -1) {
			$dom.replaceWith(i18n.get(key, index+1));
		}
	};

	i18n.setLang(lang);

	window.__   = i18n.get;
	window.i18n = i18n;
})(window);